package com.hm.jiaowuxitong.ui;

import com.hm.jiaowuxitong.util.DBUtil;
import com.hm.jiaowuxitong.util.UIUtil;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class UpdataGradeInfoUI {
    private int frame_width=700;
    private int frame_height=600;


    public UpdataGradeInfoUI(){
        //创建添加学生信息的面板
        JFrame frame=new JFrame("修改成绩信息");
        //设置面板的宽高
        frame.setSize(frame_width,frame_height);
        //禁止面板放大缩小
        frame.setResizable(false);
        //创建画布
        JPanel panel=new JPanel();
        //创建方向布局
        SpringLayout layout=new SpringLayout();
        panel.setLayout(layout);
        //创建文本框控件
        JLabel label1=new JLabel("修改人学号:");
        layout.putConstraint(SpringLayout.WEST,label1,2,SpringLayout.WEST,panel);
        layout.putConstraint(SpringLayout.NORTH,label1,5,SpringLayout.NORTH,panel);

        //创建文本编辑框控件
        JTextField jtf1=new JTextField(10);
        layout.putConstraint(SpringLayout.NORTH,jtf1,2,SpringLayout.NORTH,panel);
        layout.putConstraint(SpringLayout.WEST,jtf1,2,SpringLayout.EAST,label1);
        //创建文本框控件
        JLabel label2=new JLabel("修改课程号:");
        layout.putConstraint(SpringLayout.WEST,label2,2,SpringLayout.WEST,panel);
        layout.putConstraint(SpringLayout.NORTH,label2,5,SpringLayout.SOUTH,label1);
        //创建文本编辑框控件
        JTextField jtf2=new JTextField(10);
        layout.putConstraint(SpringLayout.WEST,jtf2,2,SpringLayout.EAST,label2);
        layout.putConstraint(SpringLayout.NORTH,jtf2,2,SpringLayout.SOUTH,jtf1);
        //创建文本框控件
        JLabel label3=new JLabel("成绩修改:");
        layout.putConstraint(SpringLayout.WEST,label3,2,SpringLayout.WEST,panel);
        layout.putConstraint(SpringLayout.NORTH,label3,5,SpringLayout.SOUTH,label2);
        //创建文本编辑框控件
        JTextField jtf3=new JTextField(10);
        layout.putConstraint(SpringLayout.WEST,jtf3,2,SpringLayout.EAST,label3);
        layout.putConstraint(SpringLayout.NORTH,jtf3,2,SpringLayout.SOUTH,jtf2);

        //创建提交按钮
        JButton button=new JButton("提交");
        layout.putConstraint(SpringLayout.WEST,button,100,SpringLayout.WEST,panel);
        layout.putConstraint(SpringLayout.SOUTH,button,2,SpringLayout.SOUTH,panel);

        //把所有控件添加至画布
        panel.add(label1);
        panel.add(jtf1);
        panel.add(label2);
        panel.add(jtf2);
        panel.add(label3);
        panel.add(jtf3);

        panel.add(button);
        JLabel img=new JLabel();
        ImageIcon imageIcon = new ImageIcon("Image/Raa.jpg");  // 将"path/to/your/image.jpg"替换为你的图片路径
        img.setIcon(imageIcon);



        panel.add(img);
        //把画布添加至面板
        frame.setContentPane(panel);
        //将面板中央显示
        new UIUtil().center(frame,frame_width,frame_height);
        //将面板显示
        frame.setVisible(true);
        //添加提交按钮的点击事件
        button.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {
                //获得所有编辑框中添加的内容信息
                String personId=jtf1.getText();
                String course=jtf2.getText();
                String grade=jtf3.getText();
                if (!personId.isEmpty()&&!course.isEmpty()&&grade.isEmpty())  {
                    System.out.println("删除成功");
                    delete(personId);
                    // 处理文本框已输入的情况
                }
                if (!personId.isEmpty()&&!course.isEmpty()&&!grade.isEmpty())  {
                    System.out.println("修改成绩成功");
                    UpdateGradeAge(grade,course,personId);
                    // 处理文本框已输入的情况
                }









            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });



    }

    public void delete(String id){
        DBUtil.jt.update("delete from 成绩表 where 学号=?", new Object[]{id});


//        System.out.println(
//                (jt.update("delete from tb_student where id=?", new Object[]{21})>0
//                        ?"删除成功":"删除失败"));
    }
    public void UpdateGradeAge(String id,String age,String grade){
        DBUtil.jt.update("update 成绩表 set 成绩=? where 学号=? and 课程=?",
                new Object[]{grade,age, id});

    }

}
